import json

from langchain_core.prompts import PromptTemplate
from pydantic import Field

from application.vk_base_mcp_v2.yuque.request import YuqueBaseRequest

code_template = """import (
	"context"
	"tongquetai_server/activity_vk/domain/manage"
	"tongquetai_server/activity_vk/infrastructure/utils"
)
const (
	ActivityTemplateType manage.EnumTemplateType = "{{activity_template_type}}"
	ActivityVersion      manage.Version          = "{{activity_version}}"
	ActivityName                                 = "{{activity_name}}"
	ActType                                      = {{act_type}}
	BigType                                      = {{big_type}}
	BigTypeName                                  = "{{big_type_name}}"
)

func getActivityName(ctx context.Context) (activityName string, err error) {
	actTypeMap, err := utils.GetTemplateActTypeMap(ctx)
	if err != nil {
		return
	}
	activityName = actTypeMap[ActType]
	return
}
"""


class ValueCodeMCPTool:

    @staticmethod
    def handler(
            activity_template_type: str = Field(..., description='活动模板类型'),
            activity_version: str = Field(..., description='活动版本'),
            activity_name: str = Field(..., description='活动名称'),
    ) -> str:
        template = PromptTemplate.from_template(
            template=code_template,
            template_format='jinja2',
        )
        type_data_map = ValueCodeMCPTool.get_act_type_data_map()
        type_data = type_data_map.get(activity_name)
        template_value = template.invoke({
            'activity_template_type': activity_template_type,
            'activity_version': activity_version,
            'activity_name': activity_name,
            'act_type': type_data.get('act_type'),
            'big_type': type_data.get('big_type'),
            'big_type_name': type_data.get('big_type_name')
        })
        return template_value.to_string()

    @staticmethod
    def get_act_type_data_map() -> dict:
        yuque_request = YuqueBaseRequest(author='shuofeng09', knowledge_base='pub4o1', doc_urls_dict={
            'activity_type_data': ['/ybg9p32a89859gts']
        })
        doc_content = yuque_request.fetch_content_and_split_by_header('activity_type_data', '# 活动类型数据')
        doc_lines = doc_content.splitlines()
        doc_lines.remove('```json')
        doc_lines.remove('```')
        result = json.loads(''.join(doc_lines))
        data = result.get('data')
        big_type_map = {}
        for item in data.get('big_type_list', []):
            big_type_map[item['type']] = item['name']
        info_map = {}
        for item in data.get('type_list', []):
            info_map[item['name']] = {
                'act_type': item['type'],
                'act_type_name': item['name'],
                'big_type': item['big_type'],
                'big_type_name': big_type_map.get(item['big_type'], '')
            }
        return info_map


if __name__ == '__main__':
    result = ValueCodeMCPTool.handler('activity_template_type', 'activity_version', '秩序之殇')
    print(result)
